﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CFM.DTO;
using CFM.Common;
using CrystalDecisions.CrystalReports.Engine;

namespace CFM
{
    public partial class frmPrintBill : Form
    {
        public frmPrintBill()
        {
            InitializeComponent();
        }

        public void LoadReport(Bill bill, List<BillDetail> details)
        {
            
            DataSet ds = new DataSet();
            ds.DataSetName = "DataSet1";
            DataTable dtBill = new DataTable();
            dtBill.TableName = "Bill";
            dtBill.Columns.Add("BillId", typeof(string));
            dtBill.Columns.Add("TableName", typeof(string));
            dtBill.Columns.Add("GroupName", typeof(string));
            dtBill.Columns.Add("Total", typeof(string));
            dtBill.Columns.Add("UserName", typeof(string));

            DataTable dtDetail = new DataTable();
            dtDetail.TableName = "BillDetail";
            dtDetail.Columns.Add("ProductName", typeof(string));
            dtDetail.Columns.Add("Price", typeof(string));
            dtDetail.Columns.Add("Quantity", typeof(string));
            dtDetail.Columns.Add("DetailTotal", typeof(string));

            dtBill.Rows.Add(
                bill.BillId,
                bill.TableName,
                bill.TableGroupName,
                string.Format(Const.NUMBER_FORMAT,bill.Total),
                Const.CurrentUser.FullName 
                );

            foreach (BillDetail detail in details)
            {
                dtDetail.Rows.Add(
                        detail.ProductName,
                        string.Format(Const.NUMBER_FORMAT,detail.Price),
                        detail.Quantity,
                        string.Format(Const.NUMBER_FORMAT,detail.DetailTotal)
                    );
            }

            ds.Tables.Add(dtBill);
            ds.Tables.Add(dtDetail);

            ReportDocument rp = new ReportDocument();
            rp.Load(PathHelper.ApplicationFolder + "\\Reports\\rpBill.rpt");
            rp.SetDataSource(ds);            
            crystalReportViewer1.ReportSource = rp;
            crystalReportViewer1.Zoom(50);
        }
    }
}
